package com.herenit.order_model.controller;

import com.herenit.order_model.entity.OmRecord;
import com.herenit.order_model.model.Message;
import com.herenit.order_model.model.ResponseVO;
import com.herenit.order_model.service.OmRecordService;
import com.herenit.order_model.service.RabbitMQService;
import com.herenit.order_model.utils.ResponseVOUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.UnsupportedEncodingException;
import java.util.List;

@RestController()
@RequestMapping(value = "/rabbit")
@Api(tags = "RabbitMQ相关接口", value = "RabbitMQ相关接口")
public class RabbitMQController {

    @Autowired
    private OmRecordService omRecordService;

    @Autowired
    private RabbitMQService rabbitMQService;

    @PostMapping(value = "/send")
    @ApiOperation("发送消息")
    public ResponseVO send(String institutionCode, String message) {
        rabbitMQService.sendMsg(institutionCode, message);
        return ResponseVOUtils.checkSendMessage(message);
    }

    @PostMapping(value = "/consume")
    @ApiOperation("消费消息")
    public ResponseVO consume(String institutionCode, String doctorCode) throws UnsupportedEncodingException {
        Message message = rabbitMQService.consumeMsg(institutionCode, doctorCode);
        return ResponseVOUtils.checkConsumeMessage(message);
    }

    @GetMapping(value = "/queryAllBack")
    @ApiOperation("打回清单")
    public ResponseVO queryAllBack(@RequestParam(defaultValue = "0") Integer page,
                                   @RequestParam(defaultValue = "10") Integer size){
        List<OmRecord> OmRecords = omRecordService.queryAllBackedRecords(page, size).getContent();
        return ResponseVOUtils.querySuccess(OmRecords);
    }
}
